<template>
  <div class="role">
    <el-dialog
      title="班次类型"
      :visible.sync="dialogVisible"
      width="800px"
      @close="closeOpen"
      :modal="true"
      :appendToBody="true"
      v-dialog-drag
      :close-on-click-modal="false"
      :top="scrollTop"
    >
      <el-form :model="queryParams" ref="queryForm" @submit.native.prevent>
        <el-row :gutter="20">
          <el-col :span="7">
            <el-form-item prop="searchValue">
              <el-input
                v-model="queryParams.query.searchValue"
                placeholder="请输入关键字"
                clearable
                size="small"
                @keyup.enter.native="searchList"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item style="display: flex">
              <el-button  icon="el-icon-search" size="mini" @click="searchList">查询</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-table
        ref="table"
        :data="classesList"
        tooltip-effect="dark"
        style="width: 100%"
        type="selection"
        v-if="dialogVisible"
        v-loading="loading"
        stripe border
      >
        <el-table-column type="selection" width="60" align="center" :selectable="selectableCallBack"/>
        <el-table-column label="班次类型编码" prop="classesCode"> </el-table-column>
        <el-table-column label="班次类型名称" prop="classesName"> </el-table-column>
        <el-table-column label="开始时间" prop="startTime"> </el-table-column>
        <el-table-column label="结束时间" prop="endTime"> </el-table-column>
      </el-table>
      <el-pagination
        v-show="total>0"
        :total="total"
        ref="pagination"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[5, 10, 20, 50, 100]"
        :page-size="queryParams.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        style="margin-top: 10px;text-align: right"
        v-if="dialogVisible"
      >
      </el-pagination>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible= false">取 消</el-button>
        <el-button type="primary" @click="beSureSelect()">保 存</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import request from '@/utils/request'
  import {listClasses} from "@/api/team/classes";
  export default {
    name: "index",
    components: {},
    data() {
      return {
        classesList: [],
        total: 0,
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          query:{
            searchValue: null
          }
        },
        loading:true,
        scrollTop: null
      }
    },
    props: {
      visibles:{
        type:Boolean,
        default:false
      },
      multiple:{
        type:Boolean,
        default:false
      },
      selectableCallBack:{
        type:Function,
        default:()=>{return true}
      },
    },
    computed: {
      dialogVisible: {
        set(val) {
          this.$emit("update:visibles", val)
        },
        get() {
          return this.visibles
        }
      }
    },

    watch: {
      'dialogVisible': function (newVal) {
        if (newVal === false) {
          this.$refs.table.clearSelection();
          this.queryParams = {
            pageNum: 1,
            pageSize: 10,
            query:{
              searchValue: null
            }
          }
        } else {
          this.getList();
        }
      },
    },

    methods: {
      beSureSelect() {
        if(!this.multiple){
          if (this.$refs.table.selection.length > 1) {
            this.$message({
              message: '最多选择一条数据!',
              type: 'warning'
            });
            return;
          }
        }
        var arr = this.$refs.table.selection || [];
        this.$emit('submitDialog', arr);
        this.dialogVisible = false;
      },

      closeOpen() {
        this.loading = false
        this.resetQuery()
      },

      //获取班次类型列表
      getList() {
        this.loading = true;
        listClasses(this.queryParams)
        .then(response => {
          if (response.code == 200) {
            this.classesList = response.rows || [];
            this.total = response.total;
          } else {
            this.classesList = []
            this.total = 0
          }
          this.loading = false;
        }).catch(() => {
          this.classesList = []
          this.total = 0
          this.loading = false;
        });
      },
      //搜索
      searchList(){
        this.resetPagination();
        this.getList();
      },
      handleNodeClick(data) {
        this.queryParams.id = data.id;
        this.getList()
      },
      handleSizeChange(num) {
        this.queryParams.pageSize = num;
        this.getList()
      },
      handleCurrentChange(num) {
        this.queryParams.pageNum = num;
        this.getList()
      },
      //重置分页
      resetPagination(){
        this.$refs.pagination.internalCurrentPage = 1;
        this.queryParams.pageNum = 1;
        this.queryParams.pageSize = 10;
      },
      //重置
      resetQuery(){
        this.queryParams.pageNum = 1;
        this.queryParams.query.searchValue = null;
        this.resetForm("queryForm");
        this.getList();
      },
    },
      mounted() {
      this.utils.mounteds(this);
    },
    destroyed() {
      this.utils.destroyeds(this);
    },
  }
</script>

<style>
  .role .vue-treeselect__control {
    height: 30px;
  }

  .role .vue-treeselect__placeholder,
  .role .vue-treeselect__single-value,
  .role .el-form-item__content,
  .role .el-form-item--medium .el-form-item__content,
  .role .el-form-item--medium .el-form-item__label {
    line-height: 30px;
  }

</style>
<style scoped>
  .el-form-item {
    margin-bottom: 10px;
  }
</style>
